<?php
/**
  * CLOUD
  *
  * This is not a freeware, use is subject to license terms
  * @author CLOUD DEV TEAM
  * 
  */
if (!defined('IN_CLOUD')) exit('Access Denied!');

class Page
{
	private $page_name="page_id";

	private $next_page;

	private $pre_page;

	private $first_page;

	private $last_page;

	private $pre_bar='<<';

	private $next_bar='>>';

	private $format_left='';

	private $format_right='';

	private $pagebarnum = 5;

	private $totalpage = 0;

	public $nowindex = 1;

	private $url="";

	private $offset = 0;

	private $rewrite = array();
	
	private $lang;
	
	public $perpage = 15;
	
	private static $object;


	public function Page( $array )
	{
		$this->next_page = "后页>";
		$this->pre_page = "<前页";
		$this->first_page = "首页";
		$this->last_page = "末页";
		if( is_array( $array ) )
		{
			if( !array_key_exists( 'total' , $array ) )
			{
		     	$this->error( __FUNCTION__ , 'need a param of total' );
			}
		
			$total=intval( $array['total'] );
		
			$this->perpage=( array_key_exists( 'perpage' , $array ) ) ? intval( $array['perpage'] ) : 10;
		
			$nowindex=( array_key_exists( 'nowindex' , $array ) ) ? intval( $array['nowindex'] ) : '';
		
			$url=( array_key_exists( 'url' , $array ) ) ? $array['url'] : '';
			
			$this->rewrite = $array['rewrite'];
		
			//$action = ( array_key_exists( 'action' , $array ) ) ? $array['action'] : '';
			//$id = ( array_key_exists( 'id' , $array ) ) ? $array['id'] : '';
			//$cat_id = ( array_key_exists( 'cat_id' , $array ) ) ? $array['cat_id'] : '';
			//$aid = ( array_key_exists( 'aid' , $array ) ) ? $array['aid'] : '';
		}
  		else
  		{
			$total = $array;
			$this->perpage = 10;
			$nowindex = '';
			$url='';
			//$action = '';
			//$id = '';
     		//$cat_id = '';
     		//$aid = '';
		}

		if( ( !is_int( $total ) ) || ( $total < 0 ) )
		{
			$this->error( __FUNCTION__ , $total . ' is not a positive integer!' );
		}
		if( ( !is_int( $this->perpage ) ) || ( $this->perpage <= 0 ) )
		{
			$this->error( __FUNCTION__ , $this->perpage . ' is not a positive integer!' );
		}
		if( !empty( $array['page_name'] ) )
		{
			$this->set( 'page_name' , $array['page_name'] );
		}
		$this->_set_nowindex( $nowindex );
		$this->_set_url( $url );
		$this->totalpage=ceil( $total / $this->perpage );
		$this->offset=( $this->nowindex - 1 ) * $this->perpage;
		//$this->action = $action;
		//$this->rewrite = array( 'action'=>$action , 'cat_id'=>$cat_id , 'aid'=>$aid );
	}
	
	public static function getInstance( $arr )
	{
		if ( !is_object( self::$object ) )
		{
			self::$object = new Page( $arr );
		}
		return self::$object;
	}

	public function set($var,$value)
	{
		if( in_array( $var , get_object_vars( $this ) ) )
		{
			$this->$var = $value;
		}
		else
		{
			$this->error( __FUNCTION__ , $var . " does not belong to PB_Page!" );
		}
	}

	public function next_page( $style='' )
	{
		if($this->nowindex < $this->totalpage)
		{
			return $this->_get_link( $this->_get_url( $this->nowindex + 1 ) , $this->next_page );
		}
		//return '<a class="' . $style . '">' . $this->next_page . '</a>';
		return '<span>' . $this->next_page . '</span>';
	}

	public function pre_page( $style = '' )
	{
		if( $this->nowindex > 1 )
		{
			return $this->_get_link( $this->_get_url( $this->nowindex - 1 ) , $this->pre_page );
		}
		//return '<a class="' . $style . '">' . $this->pre_page . '</a>';
		return '<span>' . $this->pre_page . '</span>';
	}

	public function first_page( $style = '' )
	{
		if( $this->nowindex == 1 )
		{
			//return '<a class="' . $style . '">' . $this->first_page . '</a>';
			return '<span>' . $this->first_page . '</span>';
		}
		return $this->_get_link( $this->_get_url( 1 ) , $this->first_page );
	}

	public function last_page( $style = '' )
	{
		if( $this->nowindex == $this->totalpage || $this->totalpage == 0 )
		{
			//return '<a class="' . $style . '">' . $this->last_page . '</a>';
			return '<span>' . $this->last_page . '</span>';
		}
		return $this->_get_link( $this->_get_url( $this->totalpage ) , $this->last_page );
	}


	public function nowbar( $style = '' , $nowindex_style = '' )
	{
		$plus=ceil( $this->pagebarnum / 2 );
		if( $this->pagebarnum - $plus + $this->nowindex > $this->totalpage)
		{
			$plus=( $this->pagebarnum - $this->totalpage + $this->nowindex );
		}
		$begin = $this->nowindex - $plus + 1;
		$begin = ( $begin >= 1 ) ? $begin : 1;
		$return = '';
		for( $i = $begin ; $i < $begin + $this->pagebarnum ; $i++ )
		{
			if( $i <= $this->totalpage )
			{
				if( $i != $this->nowindex )
				{
					$return.=$this->_get_text($this->_get_link($this->_get_url($i),$i));
				}
				else
				{
					//$return.=$this->_get_text('<a class="' . $nowindex_style . '">'.$i.'</a>');
					$return.=$this->_get_text('<span class="on_page">'.$i.'</span>');
				}
			}
			else
			{
				break;
			}
			$return.="\n";
		}
		unset($begin);
		return $return;
	}

	public function select()
	{
		$return = '<select name="PB_Page_Select">' ;
		for( $i = 1 ; $i <= $this->totalpage ; $i++ )
		{
			if($i == $this->nowindex)
			{
				$return .= '<option value="' . $i . '" selected>' . $i . '</option>';
			}
			else
			{
				$return .= '<option value="' . $i . '">' . $i . '</option>';
			}
		}
		unset( $i );
		$return .= '</select>';
		return $return;
	}

	public function offset()
	{
		return $this->offset;
	}


	public function show( $mode = 3 , $styleArr = array( 'first' => 'firstPage' , 'pre' => 'prePage' , 'next' => 'nextPage' , 'last' => 'lastPage' ) )
	{
		switch ( $mode )
		{
			case '1':
				return $this->pre_page().$this->nowbar().$this->next_page(). $this->lang['di'] .$this->select() . $this->lang['ye'];
			break;
	
			case '2':
				return $this->first_page().$this->pre_page() . '[' . $this->lang['di'] . $this->nowindex . $this->lang['ye'] . ']' . $this->next_page() . $this->last_page() . $this->lang['di'] . $this->select() . $this->lang['ye'];
			break;
	
			case '3':
				return $this->first_page() . $this->pre_page() . $this->nowbar( '' , 'on' ) . $this->next_page() . $this->last_page();
			break;
		
			case '4':
				return $this->pre_page() . $this->nowbar() . $this->next_page();
			break;
	
			case '5':
	    		return $this->pre_bar() . $this->pre_page() . $this->nowbar() . $this->next_page() . $this->next_bar();
			break;
		}
	}

	private function _set_url( $url = "" )
	{
		if( !empty( $url ) )
		{
			$this->url = $url . ( ( stristr( $url , '?' ) ) ? '&' : '?' ) . $this->page_name . "=";
		}
		else
		{
			if( empty( $_SERVER['QUERY_STRING'] ) )
			{
				$this->url = $_SERVER['REQUEST_URI'] . "?" . $this->page_name . "=";
			}
			else
			{
				if( stristr( $_SERVER['QUERY_STRING'] , $this->page_name . '=' ) )
				{
					$this->url = str_replace( $this->page_name . '=' . $this->nowindex , '' , $_SERVER['REQUEST_URI'] );
					$last = $this->url[strlen($this->url) - 1];
					if( $last == '?' || $last == '&' )
					{
						$this->url .= $this->page_name . "=";
					}
					else
					{
						$this->url .= '&' . $this->page_name . "=";
					}
				}
				else
				{
					$this->url = $_SERVER['REQUEST_URI'] . '&' . $this->page_name . '=';
				}
			}
		}
	}


	private function _set_nowindex( $nowindex )
	{
	    if( empty( $nowindex ) )
	    {
	        if( isset( $_GET[$this->page_name]) && $_GET[$this->page_name] > 0 )
	        {
	            $this->nowindex = intval(	$_GET[$this->page_name]	);
		    }
		    else 
		    {
		        $this->nowindex = 1;
		    }
		}
		else
		{
		    $this->nowindex=intval( $nowindex );
		}
	}

	private function _get_url( $pageno = 1 )
	{
		$isRewrite = Common::getConfig( 'System' , 'isUrlRewrite' );
		$arr = $this->rewrite;
		$arr['page_id'] = $pageno;
		if( $isRewrite && !empty( $arr['action'] ) )
		{
			return Url::rewriteUrl( $arr['action'] , $arr );
		}
		else
		{
			return $this->url . $pageno;
		}
	}

	private function _get_text($str)
	{
		return $this->format_left.$str.$this->format_right;
	}

	private function _get_link( $url , $text , $style = '' )
	{
		$style=( empty( $style ) ) ? '' : 'class="' . $style . '"';
		return '<a ' . $style . ' href="' . $url . '">' . $text . '</a>';
	}


	public function error($function,$errormsg)
	{
		die('Error in file <b>'.__FILE__.'</b> ,Function <b>'.$function.'()</b> :'.$errormsg);
	}
}
